<template>
  <div
    :id="id"
    :class="className"
    :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    data: Object
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    data: {
      handler(val) {
        if (val.troubleNum !== undefined) {
          this.$nextTick(() => {
            this.initChart()
          })
        }
      },
      deep: true
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      const options = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['操作日志', '登录日志', '错误日志']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['08:30', '08:30', '08:30', '08:30', '08:30', '08:30', '08:30', '08:30', '08:30', '08:30', '08:30', '08:30', '08:30', '08:30']
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false
          }
        },
        series: [
          {
            name: '操作日志',
            type: 'line',
            stack: '总量',
            smooth: true,
            data: [120, 132, 101, 134, 100, 130, 110, 120, 132, 101, 134, 90, 130, 110]
          },
          {
            name: '登录日志',
            type: 'line',
            smooth: true,
            stack: '总量',
            data: [220, 182, 191, 234, 290, 230, 210, 220, 182, 191, 234, 200, 230, 210]
          },
          {
            name: '错误日志',
            type: 'line',
            smooth: true,
            stack: '总量',
            data: [150, 232, 201, 154, 190, 230, 210, 150, 232, 201, 154, 190, 130, 410]
          }
        ]
      }
      this.chart.setOption(options)
    }
  }
}
</script>
